package com.bdth.weget.net.protocol;

import com.bdth.weget.util.BinaryUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.ReferenceCountUtil;

/**
 * Created by Administrator on 2018/4/19.
 */
public class JTHeader {

    private short msgType;
    private short length;
    private boolean packages;
    private boolean rsa;
    private String simno;
    private short seriaNo;
    private short allpackage;
    private short packageno;

    private short message;

    public short getMsgType() {
        return msgType;
    }

    public void setMsgType(short msgType) {
        this.msgType = msgType;
    }

    public short getLength() {
        return length;
    }

    public void setLength(short length) {
        this.length = length;
    }

    public boolean isPackages() {
        return packages;
    }

    public void setPackages(boolean packages) {
        this.packages = packages;
    }

    public String getSimno() {
        return simno;
    }

    public void setSimno(String simno) {
        this.simno = simno;
    }

    public boolean isRsa() {
        return rsa;
    }

    public void setRsa(boolean rsa) {
        this.rsa = rsa;
    }

    public short getSeriaNo() {
        return seriaNo;
    }

    public void setSeriaNo(short seriaNo) {
        this.seriaNo = seriaNo;
    }

    public short getAllpackage() {
        return allpackage;
    }

    public void setAllpackage(short allpackage) {
        this.allpackage = allpackage;
    }

    public short getPackageno() {
        return packageno;
    }

    public void setPackageno(short packageno) {
        this.packageno = packageno;
    }

    public short getMessage() {
        return message;
    }

    public void setMessage(short message) {
        this.message = message;
    }

    public byte[] write(){
        ByteBuf directBuf = Unpooled.directBuffer(16);
        directBuf.writeShort(msgType);
        directBuf.writeShort(message);
        directBuf.writeBytes(BinaryUtil.str2Bcd(simno));
        directBuf.writeShort(seriaNo);
        if(isPackages()){
            directBuf.writeShort(allpackage);
            directBuf.writeShort(packageno);
        }
        byte[] message = new byte[directBuf.readableBytes()];
        directBuf.readBytes(message);
        directBuf.release();
        return message;
    }

    public void read(byte[] bytes){

    }

    @Override
    public String toString() {
        return "JTHeader{" +
                "消息类型=" + msgType +
                ", 消息长度=" + length +
                ", 是否分包=" + (packages?"是":"否") +
                ", 是否加密=" + (rsa?"是":"否") +
                ", SIM号=" + simno +
                ", 流水号=" + seriaNo +
                (packages?", 总包数=" + allpackage +", 包序号=" + packageno :"") +
                '}';
    }
}
